package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.domain.vo.CommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.PublishApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {
    @Reference
    private PublishApi publishApi;
    @Reference
    private CommentApi commentApi;
    @Reference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private  MovementsMQService movementsMQService;

    //    * 评论列表
    /**
     * 查询某一动态的文字评论列表
     * @param movementId 动态id
     * @param page 页码
     * @param pagesize 每页几条
     */
    public ResponseEntity findCommentsList(String movementId, int page, int pagesize) {
        //查询id所对应的评论信息
        PageResult<Comment> commentPageResult = commentApi.findComments(page, pagesize, movementId);
        //转换成vo
        List<Comment> commentList = commentPageResult.getItems();
        List<CommentVO> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            CommentVO vo = new CommentVO();
            //把评论信息封装vo对象里
            BeanUtils.copyProperties(comment,vo);
            vo.setId(comment.getId().toHexString());
            //  yyyy年MM月dd日 HH:mm
            String format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm").format(new Date(comment.getCreated()));
            vo.setCreateDate(format);
            //把评论者的数据封装到vo
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            BeanUtils.copyProperties(userInfo,vo);
            //当前用户是否对这条评论点赞了
            Boolean hasLike = redisTemplate.hasKey("comment_like_" + UserHolder.getUserId() + "_" + comment.getId().toHexString());
            vo.setHasLiked(hasLike?1:0);
            voList.add(vo);


        }
      // 构造返回值
        PageResult<CommentVO> vopageResult = new PageResult<>();
        BeanUtils.copyProperties(commentPageResult,vopageResult);
        vopageResult.setItems(voList);
        return ResponseEntity.ok(vopageResult);

    }
//发表动态

    public ResponseEntity saveComment(String publishId, String content) {
        //补全数据
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId));
        comment.setCommentType(2);
        comment.setPubType(1);
        comment.setContent(content);
        comment.setUserId(UserHolder.getUserId());

        //设置被评论的用户
        Publish publish = publishApi.findById(publishId);
        comment.setPublishUserId(publish.getUserId());


        //保存评论
        commentApi.save(comment);
        //发送消息
        movementsMQService.savePublishMessage(publishId);


        return ResponseEntity.ok(null);
    }
}
